import { logger } from "../log";
import { WsMsg, WsMsgType } from "../model/WebSocket";
import { wsStore } from "./store";

export function sendToClientAll<T extends WsMsgType, R>(param: WsMsg<T, R>) {
  wsStore.getClients().map((v) => {
    v.send(JSON.stringify(param), (err) => {
      if (err) logger.error(`WS send error: ${err}`);
    });
  });
}

export function sendToClient<T extends WsMsgType, R>(
  clientId: string,
  param: WsMsg<T, R>
) {
  wsStore.get(clientId)?.send(JSON.stringify(param), (err) => {
    if (err) logger.error(`WS send error: ${err}`);
  });
}
